package gui.visu.listeners;

import gui.utils.components.DisplayDataPopupWindow;
import gui.visu.components.popupdisplaydata.A_PopupDisplayData;

import java.awt.event.MouseEvent;

import tuio.TwoFingersEvent;

import controls.visu.BetweenControl;
import data.visu.structures.PairOfDouble;
import data.visu.structures.Range;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class ListenerPopupBetween extends A_ListenerPopupDisplayData{


	private int value2=0;

	public ListenerPopupBetween(BetweenControl controler, A_PopupDisplayData popupDisplayData){
		super(controler, popupDisplayData);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getButton()==MouseEvent.BUTTON1){
			if(e.getClickCount()==2){
				((popupDisplayData.getParent().getParent().getParent().getParent().getParent())).setEnabled(true);
				((DisplayDataPopupWindow)(popupDisplayData.getParent().getParent().getParent().getParent())).dispose();
			}
		}
	}

	@Override
	public void mousePressed(MouseEvent me) {
		if(me instanceof TwoFingersEvent){
			TwoFingersEvent e = (TwoFingersEvent)me;
			if(e.getY()>e.getY2()){
				xStart = e.getX2();
				xEnd = e.getX2();
			}
			else{
				xStart = e.getX();
				xEnd = e.getX();
			}
		}
	}

	@Override
	public void mouseDragged(MouseEvent me) {
		if(me instanceof TwoFingersEvent){
			TwoFingersEvent e = (TwoFingersEvent)me;
			boolean leftToRight;
			if(e.getY()>e.getY2()){
				if(e.getX2()>xStart){
					xEnd = e.getX2();
					if(xEnd<0)xEnd=0;
					else if(xEnd>popupDisplayData.getWidth())xEnd = popupDisplayData.getWidth();
					leftToRight=true;
				}
				else{
					xStart = e.getX2();
					if(xStart<0)xStart=0;
					else if(xStart>popupDisplayData.getWidth())xStart = popupDisplayData.getWidth();
					leftToRight=false;
				}
				value = popupDisplayData.getHeight()-e.getY();
				value2 = popupDisplayData.getHeight()-e.getY2();
			}
			else{
				if(e.getX()>xStart){
					xEnd = e.getX();
					if(xEnd<0)xEnd=0;
					else if(xEnd>popupDisplayData.getWidth())xEnd = popupDisplayData.getWidth();
					leftToRight=true;
				}
				else{
					xStart = e.getX();
					if(xStart<0)xStart=0;
					else if(xStart>popupDisplayData.getWidth())xStart = popupDisplayData.getWidth();
					leftToRight=false;
				}
				value = popupDisplayData.getHeight()-e.getY2();
				value2 = popupDisplayData.getHeight()-e.getY();
			}
			if(value<0)value=0;
			else if(value>popupDisplayData.getHeight())value = popupDisplayData.getHeight();
			if(value2<0)value=0;
			else if(value2>popupDisplayData.getHeight())value2 = popupDisplayData.getHeight();

			int xMin, xMax;
			xMin = popupDisplayData.convertXFromRefToValue(xStart);
			xMax = popupDisplayData.convertXFromRefToValue(xEnd);

			if(xMax>xMin){
				PairOfDouble yValue = new PairOfDouble(popupDisplayData.convertYFromRefToValueForDouble(value), popupDisplayData.convertYFromRefToValueForDouble(value2));
				controler.getDataSet().addRange2Map(new Range(xMin,xMax,controler),yValue);
				if(leftToRight)xStart = xEnd;
				else xEnd=xStart;
				popupDisplayData.repaint();
				controler.getDisplayData().repaint();
			}
		}
	}
}
